<template>
  <ele-page>
    <!-- 搜索表单 -->
    <search @search="reload" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        highlight-current-row
        stripe
        border
        :where="defaultWhere"
      >
        <template #buy="{ row }">
          <span class="pointer blue" @click="openOffer(row)">查看</span>
        </template>
        <template #refund_images="{ row }">
          <span
            v-if="row.refund_images !== ''"
            class="pointer blue"
            @click="openImages(row, 'refund_images')"
          >
            查看
          </span>
          <span v-else>无</span>
        </template>
        <template #status="{ row }">
          <el-tag :type="row.statusType">
            {{ row.statusText }}
          </el-tag>
        </template>
        <template #driver="{ row }">
          <span
            v-if="row.refund_to_driver == 1"
            class="pointer blue"
            @click="openImages(row, 'refund_to_driver_images')"
          >
            {{ row.refund_to_driver_amount }}
          </span>
          <span v-else>不退</span>
        </template>
        <template #refund_pdf="{ row }">
          <span
            v-if="row.refund_pdf !== ''"
            class="pointer blue"
            @click="openPdf(row.refund_pdf)"
            >查看</span
          >
          <span v-else>无</span>
        </template>
        <template #action="{ row }">
          <el-button-group>
            <el-button type="success" size="small" @click="openInfo(row)">
              填写退保信息
            </el-button>
          </el-button-group>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 退保信息填写 -->
    <info v-model="showInfo" :data="current" @done="reload" />
    <!-- 图片 -->
    <images
      v-model="showImages"
      :data="current"
      :read-only="true"
      :img-type="imgType"
    />
  </ele-page>
</template>

<script setup>
  import { ref, computed } from 'vue';
  import { pageInsuranceRefund } from '@/api/company';
  import Search from './components/search.vue';
  import Info from './components/info.vue';
  import Images from './components/images.vue';

  /** 表格实例 */
  const tableRef = ref(null);
  const defaultWhere = {
    status: 0
  };

  /** 表格列配置 */
  const columns = computed(() => {
    const cols = [];

    cols.push(
      {
        width: 70,
        prop: 'id',
        label: 'ID'
      },
      {
        width: 100,
        align: 'center',
        slot: 'status',
        label: '状态'
      },
      {
        width: 100,
        align: 'center',
        prop: 'create_user',
        label: '退保创建人'
      },
      {
        width: 140,
        align: 'center',
        prop: 'create_time',
        label: '创建时间'
      },
      {
        prop: 'refund_reason',
        label: '退保原因'
      },
      {
        width: 90,
        align: 'center',
        slot: 'refund_images',
        label: '相关图片'
      },
      {
        width: 120,
        prop: 'plate_number',
        label: '相关车牌'
      },
      {
        width: 120,
        prop: 'cate',
        label: '保险种类'
      },
      {
        width: 180,
        prop: 'insurance',
        label: '保险公司'
      },
      {
        width: 100,
        prop: 'insurance_salesman',
        label: '业务员'
      },
      {
        width: 140,
        prop: 'refund_date',
        label: '实际退保日期'
      },
      {
        width: 100,
        prop: 'refund_amount',
        label: '退保金额'
      },
      {
        width: 100,
        prop: 'refund_rebate',
        label: '返还佣金'
      },
      {
        width: 100,
        slot: 'refund_pdf',
        label: '退保批单'
      },
      {
        width: 100,
        prop: 'done_user',
        label: '退保完成'
      },
      {
        width: 140,
        prop: 'done_time',
        label: '完成时间'
      }
    );
    if (status.value == 0) {
      cols.push({
        width: 130,
        slot: 'action',
        label: '操作',
        align: 'center',
        fixed: 'right'
      });
    } else {
      // 不是待处理都显示退给司机多少钱
      cols.push({
        width: 100,
        slot: 'driver',
        label: '退还车主',
        align: 'center'
      });
    }

    return cols;
  });

  /** 表格数据源 */
  const datasource = async ({ page, limit, where, orders }) => {
    const result = await pageInsuranceRefund({
      ...where,
      ...orders,
      page,
      limit
    });
    return result;
  };
  const status = ref(0);
  /** 搜索 */
  const reload = (where) => {
    status.value = where ? where.status : 0;
    tableRef.value?.reload?.({ page: 1, where });
  };

  const current = ref([]);

  const openPdf = (url) => {
    window.open(url);
  };

  /** 查看图片 */
  const imgType = ref('');
  const showImages = ref(false);
  const openImages = (data, type) => {
    imgType.value = type;
    current.value = data;
    showImages.value = true;
  };

  /** 查看出单要求 */
  const showOffer = ref(false);
  const openOffer = (row) => {
    current.value = row;
    showOffer.value = true;
  };

  /** 填写退保信息 */
  const showInfo = ref(false);
  const openInfo = (row) => {
    current.value = row;
    showInfo.value = true;
  };
</script>
